package managedbean.SCM.Supplier;

import entity.SCM.Supplier;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.stateless.SCM.SupplierBeanLocal;

@ManagedBean(name="viewSupplier")
@ViewScoped
public class ViewSupplierManagedBean {

    public ViewSupplierManagedBean() {
    }
    @EJB
    private SupplierBeanLocal supplierBeanLocal;
    private Supplier selectedSupplier;
    private List<Supplier> suppliers;
    private String statusMessage;

    public Supplier getSelectedSupplier() {
        return selectedSupplier;
    }

    public void setSelectedSupplier(Supplier selectedSupplier) {
        this.selectedSupplier = selectedSupplier;
    }

    public List<Supplier> getSuppliers() {
        this.suppliers = supplierBeanLocal.getAllSupplier();
        return suppliers;
    }
  
    public void editSupplier(ActionEvent event) throws Exception{
        Supplier supplier = this.getSelectedSupplier();
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedSupplier", supplier);
        FacesContext.getCurrentInstance().getExternalContext().redirect("editSupplier.xhtml");            
    }
    
    public void deleteSupplier(ActionEvent event) throws Exception{
        try{
            supplierBeanLocal.deleteSupplier(selectedSupplier.getSupplierName());
            FacesContext.getCurrentInstance().getExternalContext().redirect("viewSupplier.xhtml");
            statusMessage = "Supplier Deleted Successfully";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
        }catch(Exception ex){
            ex.printStackTrace();
        }        
    }
 
}
